package com.fho4565.core.timer;

import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;

import java.io.Serial;
import java.util.HashSet;

public class TimerList extends HashSet<Timer> implements Cloneable {

    @Serial
    private static final long serialVersionUID = -2089790793772345938L;

    public TimerList() {
        super();
    }

    public Timer getByName(String name){
        return this.stream().filter(timer -> timer.getName().equals(name)).findFirst().orElse(null);
    }
    public void tick(CommandSourceStack commandSourceStack,Commands commands){
        for(Timer timer : this){
            if(timer.countDown()){
                commands.performCommand(timer.isSilent() ? commandSourceStack.withSuppressedOutput() : commandSourceStack, timer.getCommand());
            }
        }
    }
    public void remove(String name){
        this.removeIf(timer -> timer.getName().equals(name));
    }
    public boolean contains(String name){
        return this.stream().anyMatch(timer -> timer.getName().equals(name));
    }
    @Override
    public Object clone() {
        TimerList result;
        result = (TimerList)super.clone();
        result.clear();
        result.addAll(this);
        return result;
    }
}
